<?php
  session_start();
  if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 36000) {
	$kask=$yhendus->prepare("DELETE FROM  broneering WHERE broneering_id=? and klient_id IS NULL");
	$kask->bind_param("i", $_SESSION["broneerin"]);
	$kask->execute();

	$kask=$yhendus->prepare("DELETE FROM  elamine WHERE broneering_id=? and klient_id IS NULL");
	$kask->bind_param("i", $_SESSION["broneerin"]);
	$kask->execute();
    // session started more than 30 minutes ago
    { session_destroy(); header("Location: registreerimine.php"); 
}
  if(!isSet($_SESSION["broneerin"])){
    header("Location: registreerimine.php");
    exit();
  }}
  /*
  if(isSet($_REQUEST["klient_id"])){   
    $_SESSION ["kid"]=$_REQUEST["klient_id"];  
          }
          */
?>
<?php 
require_once("konf.php"); 
if(isSet($_REQUEST["kustutusid"])){
 $kask=$yhendus->prepare("DELETE FROM elamine WHERE elamine_id=?");
 $kask->bind_param("i", $_REQUEST["kustutusid"]);
 $kask->execute();
 }
?>
<?php echo $_SESSION["ruumiliik"]?>
<?php 
  require_once("konf.php"); 
  if(isSet($_REQUEST["andmed"])){
    $kask=$yhendus->prepare(
        "INSERT INTO klient (klient_id, nimi, isikukood, telefon, mail) VALUES (?,?,?, ?, ?);");
        echo $yhendus->error;
        $kask->bind_param("isiss",$_REQUEST["klient_id"], $_REQUEST["nimi"], $_REQUEST["isikk"], $_REQUEST["tel"], $_REQUEST["mail"]);
        $kask->execute();
        $_SESSION["kid"]=$yhendus->insert_id;
        //header("Location: klient.php?id=$_REQUEST[id]&uusid=$yhendus->insert_id");
        
        $kask=$yhendus->prepare("UPDATE broneering SET klient_id=? WHERE broneering_id=?");
        $kask->bind_param("ii", $_SESSION["kid"],$_SESSION["broneerin"]);
        $kask->execute();

        $kask=$yhendus->prepare(
        "INSERT INTO elanikud(broneering_id,klient_id, eesnimi, perekonnanimi, isikukood) VALUES (?,?, ?, ?, ?)");
        echo $yhendus->error;
    $i=0;
     while(!empty($_REQUEST["eesn"][$i])){
        $kask->bind_param("iisss",$_SESSION["broneerin"],$_SESSION["kid"], $_REQUEST["eesn"][$i], $_REQUEST["peren"][$i], $_REQUEST["isikukood"][$i]);
        $kask->execute();
        $i++;
		header("Location: tehtud.php");
}
 if(isSet($_REQUEST["kustutusid"])){
 $kask=$yhendus->prepare("DELETE FROM elamine WHERE elamine.elamine_id=?");
 $kask->bind_param("i", $_REQUEST["kustutusid"]);
 $kask->execute();
 }
exit(); 
  }
   $kask=$yhendus->prepare("SELECT elamine.elamine_id, ruumiliigid.liik, elamine.algusaeg, elamine.lopuaeg, hinnad.hind, kohtadearv FROM elamine, broneering, ruumiliigid, hinnad 
   WHERE broneering.broneering_id=? and broneering.broneering_id=elamine.broneering_id and ruumiliigid.hinnad_id=hinnad.hinnad_id 
   and elamine.ruumiliigid_id=ruumiliigid.ruumiliigid_id; ");
   $kask->bind_param("i", $_SESSION["broneerin"]);
  $kask->bind_result ($id, $liik, $algusaeg, $lopuaeg, $hind, $kohtadearv);
  $kask->execute();

?>


<!doctype html>
<html>
  <head>
    <title>Ülevaade</title>
                <link rel="stylesheet" type="text/css" href="kujundus.css"/>
  </head>
  <header>
  <div id="firma">
  <h1>IFI Grand Hotell</h1>
  </div>
  
  </header>
  <body>
<section class="menyy">
<ul > 1. Valige oma toad</ul>
<ul style="color:white; text-shadow:0px 0px 5px rgba(255,255,255, 1);">2. Esitage oma andmed</ul>
<ul>3. Tehtud</ul>

</section>
<section id="keha2">


        <h2>Broneeringu ülevaade</h2>
 <table>
<table border="1">
   <colgroup>
        <col span="1">
        <col span="1">
   </colgroup>
<thead> 
        <tr>
		
                <td colspan="1">Ruumiliik</td>
                <td colspan="1">Saabumine</td>
                <td colspan="1">Lahkumine</td>
                <td colspan="1">Hind</td>
				<td colspan="1">Hind kokku</td>
                <td colspan="1">Kohtade arv</td>
                
        </tr>
   </thead>
   </thead>
                    <?php
		  date_default_timezone_set('Europe/Helsinki');
		  while($kask->fetch()){
	$start=strtotime($algusaeg);
	$end=strtotime($lopuaeg);
	$datediff = ceil(abs($end - $start) / 86400);
	$hindkokku=($hind*$datediff);
                  echo "
                    <tr>
					
                        <td>$liik</td>
                        <td>$algusaeg</td>
                        <td>$lopuaeg</td>
                        <td>$hind €</td>
						<td>$hindkokku €</td>
                        <td>$kohtadearv</td>
						<td><a href='?kustutusid=$id'>kustuta</a></td>
                          </td>
                        </tr>
                  ";
                }
          ?>
        </table>
<form action="?">
<input type="hidden" name="andmed" />
<input type="hidden" name=$klient_id/>
<p></p>

<table border='1'>
<thead>
<td colspan='4'>Teie andmed</td>
<thead>
<tbody>
<tr>    
        <td>Nimi: 
        <input type='text' name="nimi"></td>
        <td>Isikukood: 
        <input type='text' name="isikk"></td></tr><tr>
        <td>Telefon: 
        <input type='text' name="tel"></td>
        <td>E-mail: 
        <input type='text' name="mail"></td>
        </tr>
</tbody>
</table>
<p></p>
<table border='1'>
<thead>
<td colspan='4'>Elanike andmed</td>
<thead>
<tbody>
<?php 
$kask=$yhendus->prepare("SELECT kohtadearv, SUM(kohtadearv) FROM elamine, ruumiliigid 
   WHERE broneering_id=? and elamine.ruumiliigid_id=ruumiliigid.ruumiliigid_id
  ; ");
   $kask->bind_param("i", $_SESSION["broneerin"]);
  $kask->bind_result ($kohtadearv, $kogus2);
  $kask->execute();

while($kask->fetch()){
$x=1;
do
{
echo "

        <tr>
                                
                <td>$x</td>
                <td>Eesnimi: 
                <input type='text' name='eesn[]'></td>
                <td>Perekonnanimi: 
                <input type='text' name='peren[]'></td>
                <td>Isikukood: 
                <input type='text' name='isikukood[]'></td>
				
				
        </tr>";
        $x++;
        }
while ($x<=$kogus2);
}?>
                </tbody>
                </table>

<input type="submit" value="Sisesta" />
</form>
</body>
</section>
</html>